package com.zretc.kscxmz.controller;


import com.github.pagehelper.PageInfo;
import com.zretc.kscxmz.entity.Account;
import com.zretc.kscxmz.services.AccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/account")
public class AccountController {
    private Logger logger = LoggerFactory.getLogger(AccountController.class);
    @Autowired
    private AccountService accountService;

    @RequestMapping("/login")
    @ResponseBody
    public String Login(HttpServletRequest request,HttpSession session,
                      @RequestParam("accountName")String accountName,
                        @RequestParam("password")String password,
                        @RequestParam("code") String code)throws IOException{
        Map<String, String> accountMap = accountService.login(accountName, password);
        code=code.toLowerCase();
        String checkCode = session.getAttribute("code").toString();
        logger.info("============="+checkCode);
        if (accountMap!=null && checkCode.equals(code)){
            session = request.getSession();
            session.setAttribute("accountMap", accountMap);
            return "0";
        }else if (accountMap!=null && checkCode!=code){
            return "1";
        }else {
            return "2";
        }

    }
    @RequestMapping(value = "/logout")
    public String accountLogout(HttpServletRequest request){
        // 清除session信息
        HttpSession session = request.getSession();
        session.removeAttribute("accountMap");

        return "/login/login";
    }
    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        logger.info("===============");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        // 生成随机字串
            String verifyCode = VerifyCodeUtils.generateVerifyCode(4);

        logger.info("=========="+verifyCode);
        // 生成图片
        int w = 135, h = 40;
        VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);

         //将验证码存储在Cookie以便登录时校验
        session.setAttribute("code",verifyCode.toLowerCase());
    }
    @RequestMapping("/getCode")
    @ResponseBody
    public String getCode(HttpSession session){
        String code = (String) session.getAttribute("code");
        logger.info("================="+code);

        return  code;
    }
    @RequestMapping("/getAccountByPage")
    public @ResponseBody
    PageInfo<Account> getAccountByPage(@RequestParam("pageNum")String pageNum,
                                       @RequestParam("pageSize")String pageSize){
        logger.info("======查询用户列表======");
        logger.info("====pageNum:"    + pageNum);
        logger.info("====pagleSize:"   + pageSize);

        Map<String, String> map = new HashMap<>();
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        //调用业务层
        PageInfo<Account> AccountByPage = accountService.getAccountByPage(map);
        logger.info("====AccountByPage:"   + AccountByPage);
        return AccountByPage;

    }

    @RequestMapping("/getAccountById")
    public @ResponseBody
    Account getAccountById(@RequestParam(value = "accountId")Integer accountId){
        logger.info("=====通过用户ID查询用户信息======");
        logger.info("====accountId:"    + accountId);

        //调用业务层
        Account AccountByPage = accountService.getAccountInfoById(accountId);
        logger.info("====AccountByPage:"   + AccountByPage);
        return AccountByPage;
    }

    @RequestMapping("/deleteAccountInfo")
    public @ResponseBody
    int deleteAccountInfo(@RequestParam(value = "accountId")String accountId){
        logger.info("=====通过用户ID删除用户信息======");
        logger.info("====Id:"    + accountId);

        //调用业务层
        int Id = Integer.parseInt(accountId);
        int AccountByPage = accountService.deleteAccountInfo(Id);
        return AccountByPage;
    }



    @RequestMapping("/updateAccountInfo")
    public @ResponseBody
    int updateAccountInfo(@RequestParam(value = "realName")String realName,
                          @RequestParam(value = "accountName")String accountName,
                          @RequestParam(value = "accountId")int accountId,
                          @RequestParam(value = "roleName")String roleName,
                          @RequestParam(value = "deptName")String deptName,
                          @RequestParam(value = "accountTel")String accountTel,
                          @RequestParam(value = "salary")float salary){

        int deptId = accountService.getDeptIdByName(deptName);
        System.out.println(deptId);
        int roleId = accountService.getRoleIdByName(roleName);
        System.out.println(roleId);
        logger.info("=====通过用户ID修改用户信息======");
        logger.info("====realName:"  + realName);
        logger.info("====accountName:"    + accountName);
        logger.info("====roleName:"  + roleName+"=="+roleId);
        logger.info("====deptName:"    + deptName+"=="+deptId);
        logger.info("====accountTel:"  + accountTel);
        logger.info("====salary:"  + salary);
        logger.info("====accountId:"  + accountId);

        //调用业务层
        Map<String, Object> map = new HashMap<>();
        map.put("realName",realName);
        map.put("accountId",accountId);
        map.put("accountName",accountName);
        map.put("roleId",roleId);
        map.put("deptId",deptId);
        map.put("accountTel",accountTel);
        map.put("salary",salary);
        int AccountByPage = accountService.updateAccountInfo(map);
        logger.info("====AccountByPage:"   + AccountByPage);
        return AccountByPage;
    }


    @RequestMapping("/toMakeSure")
    public @ResponseBody
    ResponseEntity addAccountInfo(@RequestParam(value = "accountName")String accountName,
                                  @RequestParam(value = "password")String password){

        logger.info("======查找用户信息======");
        logger.info("====accountName:"  + accountName);
        logger.info("====password:"  + password);

        //调用业务层
        Map<String, Object> map = new HashMap<>();
        map.put("accountName",accountName);
        map.put("password",password);

        Account Account = accountService.toMakeSure(map);
        logger.info("====Account:"   + Account);
        if(Account !=null ){
            return  new ResponseEntity(Account, HttpStatus.OK);
        }
        return new ResponseEntity(null, HttpStatus.NO_CONTENT);
    }

    @RequestMapping("/addAccountInfo")
    public @ResponseBody
    int addAccountInfo(@RequestParam(value = "realName")String realName,
                       @RequestParam(value = "accountName")String accountName,
                       @RequestParam(value = "roleName")String roleName,
                       @RequestParam(value = "deptName")String deptName,
                       @RequestParam(value = "accountTel")String accountTel,
                       @RequestParam(value = "salary")float salary){

        int deptId = accountService.getDeptIdByName(deptName);
        System.out.println(deptId);
        int roleId = accountService.getRoleIdByName(roleName);
        System.out.println(roleId);
        logger.info("=====通过增加用户信息======");
        logger.info("====realName:"  + realName);
        logger.info("====accountName:"    + accountName);
        logger.info("====roleName:"  + roleName+"=="+roleId);
        logger.info("====deptName:"    + deptName+"=="+deptId);
        logger.info("====accountTel:"  + accountTel);
        logger.info("====salary:"  + salary);


        //调用业务层
        Map<String, Object> map = new HashMap<>();
        map.put("realName",realName);
        map.put("accountName",accountName);
        map.put("roleId",roleId);
        map.put("deptId",deptId);
        map.put("accountTel",accountTel);
        map.put("salary",salary);
        int AccountByPage = accountService.addAccountInfo(map);
        logger.info("====AccountByPage:"   + AccountByPage);
        return AccountByPage;
    }

    @RequestMapping("/motifyPwdByid")
    public
    String motifyPwdByid(@RequestParam(value = "accountId")int accountId,
                         @RequestParam(value = "password")String password,
                         HttpServletRequest request){

        logger.info("======修改用户密码======");
        logger.info("====AccountId:"  + accountId);
        logger.info("====password:"  + password);

        //调用业务层
        Map<String, Object> map = new HashMap<>();
        map.put("accountId",accountId);
        map.put("password",password);

        int Account = accountService.motifyPwdByid(map);
        logger.info("====Account:"   + Account);
        HttpSession session = request.getSession();
        session.removeAttribute("accountMap");

        return "/login/login";


    }


    @RequestMapping("/motifyInfoByid")
    public @ResponseBody
    int motifyInfoByid(@RequestParam(value = "accountName")String accountName,
                       @RequestParam(value = "accountTel")String accountTel,
                       @RequestParam(value = "accountSchool")String accountSchool,
                       @RequestParam(value = "accountEdu")String accountEdu,
                       @RequestParam(value = "address")String address,
                       @RequestParam(value = "accountId")int accountId){


        logger.info("=====修改用户信息======");
        logger.info("====accountName:"  + accountName);
        logger.info("====accountTel:"    + accountTel);
        logger.info("====accountSchool:"  + accountSchool);
        logger.info("====accountEdu:"  + accountEdu);
        logger.info("====address:"  + address);
        logger.info("====accountId:"  + accountId);


        //调用业务层
        Map<String, Object> map = new HashMap<>();
        map.put("accountName",accountName);
        map.put("accountTel",accountTel);
        map.put("accountEdu",accountEdu);
        map.put("accountSchool",accountSchool);
        map.put("address",address);
        map.put("accountId",accountId);
        int motifyInfoByid = accountService.motifyInfoByid(map);
        logger.info("====motifyInfoByid:"   + motifyInfoByid);
        return motifyInfoByid;
    }

    @RequestMapping("/getInfoById")
    public @ResponseBody
    Account getInfoById(
            @RequestParam(value = "accountId")int accountId ){


        logger.info("=====用户列表信息======");
        logger.info("====accountId:"    + accountId);


        //调用业务层

        Account infoById = accountService.getInfoById(accountId);
        logger.info("====infoById:"   + infoById);
        return infoById;
    }
}
